import { t } from "./analysis";
import { getNodeWithPathAtPosition } from "./util";
import { Location } from "vscode-languageserver";
import { pathToUri } from "./translation";

export async function proviedReferences(source: t.SourceFile, position: number, includeDefinition: boolean = false): Promise<Location[] | null> {
    let nodeResult = getNodeWithPathAtPosition(source, position);
    let symbol = nodeResult.node?.symbol;

    if (!nodeResult.node || !symbol) {
        return null;
    }

    let locations: Location[] = [];
    symbol.tracker.forEachNode(node => {
        const file = node.getSourceFile();
        if (file && (node !== symbol!.declaration.name || includeDefinition)) {
            locations.push(Location.create(
                pathToUri(file.fileName),
                {
                    start: file.getLineAndCharactorOfPosition(node.start),
                    end: file.getLineAndCharactorOfPosition(node.end)
                }
            ));
        }
    });
    return locations;
}
